package com.giaothong;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

import com.giaothong.entities.Score;
import com.giaothong.utils.ExpAdapter;

public class Expextends extends Activity {

	DisplayMetrics metrics;
	int width;
	ExpandableListView expList;
	private String arrGroupElements[];
	private String arrChildElements[][];
	private List<Score> listScoreFromHistory;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_lichsu);

		expList = (ExpandableListView) findViewById(R.id.history_expandablelist);
		arrGroupElements = new String[Main.getScoreDAL().getCurrentSizeOfHistory()];
		arrChildElements = new String[Main.getScoreDAL().getCurrentSizeOfHistory()][5];
		listScoreFromHistory = Main.getScoreDAL().getScoreHistory();
		getValueToExp(listScoreFromHistory);
		
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		width = metrics.widthPixels;
		
		
		// this code for adjusting the group indicator into right side of the view
		expList.setIndicatorBounds(width - GetDipsFromPixel(50), width- GetDipsFromPixel(10));
		expList.setAdapter(new ExpAdapter(this, arrGroupElements, arrChildElements));
		expList.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int groupPosition) {
				Log.e("onGroupExpand", "OK");
			}
		});

		expList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int groupPosition) {
				Log.e("onGroupCollapse", "OK");
			}
		});

		expList.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Log.e("OnChildClickListener", "OK");
				return false;
			}
		});
	}
	
	private void getValueToExp(List<Score> listScore){
		int indexGroup = 0;
		for(Score score : listScore){
			arrGroupElements[indexGroup] = " " + score.getTime();
			arrChildElements[indexGroup][0] = " " + getResources().getString(R.string.topic) + score.getTopicTitle();
			arrChildElements[indexGroup][1] = " " + getResources().getString(R.string.result)+ score.getCorrect() + "/" + score.getSumOfQuestions();
			arrChildElements[indexGroup][2] = " " + getResources().getString(R.string.topic_cover_duration)+ score.getDurationFormat();
			arrChildElements[indexGroup][3] = " " + getResources().getString(R.string.unanswer) + score.getUnanswered();
			indexGroup ++;
		}
	}
		
	public int GetDipsFromPixel(float pixels) {
		// Get the screen's density scale
		final float scale = getResources().getDisplayMetrics().density;
		// Convert the dps to pixels, based on density scale
		return (int) (pixels * scale + 0.5f);
	}
}